import utils from './index.js'
function getPathSep(path) {
  return path.indexOf('/') === -1 && path.indexOf('\\') !== -1 ? '\\' : '/'
}
function parseLocalPath(path) {
  const obj = {}
  const sep = getPathSep(path)
  const parts = path.split(sep)
  let i = 0
  if (parts.length === 1) {
    obj.filename = parts[0]
    obj.directory = ''
  } else {
    obj.filename = parts.pop()
    obj.directory = parts.join(sep)
  }
  i = obj.filename.lastIndexOf('.')
  if (i > -1) {
    obj.extension = obj.filename.substr(i + 1)
    obj.basename = obj.filename.substr(0, i)
    obj.pathbase = path.substr(0, path.lastIndexOf('.'))
  } else {
    obj.extension = ''
    obj.basename = obj.filename
    obj.pathbase = path
  }
  return obj
}
function getFileExtension(path) {
  return parseLocalPath(path).extension
}
function getFileBase(path) {
  return parseLocalPath(path).basename
}
function getPathBase(path) {
  return parseLocalPath(path).pathbase
}
function getCommonFileBase(names) {
  return names.reduce((memo, name, i) => {
    if (i === 0) {
      memo = getFileBase(name)
    } else {
      memo = utils.mergeNames(memo, name)
    }
    return memo
  }, "")
}
export default {
  parseLocalPath,
  getFileExtension,
  getFileBase,
  getPathBase,
  getCommonFileBase,
}